package com.e_manager.soft.emanager.service.impl;

import com.e_manager.soft.emanager.entity.User;
import com.e_manager.soft.emanager.entity.parameter.UserEquipmentParameter;
import com.e_manager.soft.emanager.idao.UserDao;
import com.e_manager.soft.emanager.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : lip[lip4051@163.com]
 * @create : 2018/3/13 0013 下午 2:58
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    /**
     * 通过用户id查询用户信息
     *
     * @param id 用户id
     * @return User 用户信息
     * @see com.e_manager.soft.emanager.entity.User
     */
    @Override
    public User getByUserId(String id) {
        if (id == null || "".equals(id)) {
            return null;
        }
        User user = userDao.getByUserId(id);
        return user;
    }

    /**
     * 通过用户id查询用户信息
     *
     * @param id 用户id
     * @return User 用户信息
     * @see com.e_manager.soft.emanager.entity.User
     */
    @Override
    public User getByUserInfor(String id) {
        if (id == null || "".equals(id)) {
            return null;
        }
        User user = userDao.getByUserInfor(id);
        return user;
    }

    @Transactional
    @Override
    public void inserteUser(User user) {
        /**
         * @author lip
         * @description
         * @method inserteUser
         * @param [user]
         * @returnType void
         * @date 2018/3/15 0015 下午 1:28 
         */
        if (user != null) {
            userDao.inserteUser(user);
        }
    }

    /**
     * 通过用户登录名查询用户信息
     *
     * @param userName 用户登录名
     * @return User 用户信息
     * @see com.e_manager.soft.emanager.entity.User
     */
    @Override
    public User getByUserName(String userName) {
        if (userName == "" || "".equals(userName.trim())) {
            return null;
        }
        User user = userDao.getByUserName(userName);
        return user;
    }

    /**
     * 通过用户登录名更新token和tokenTime phone password
     *
     * @param user user信息
     */
    @Transactional
    public void updateUser(User user) {
        if (user != null) {
            userDao.updateUser(user);
        }
    }

    /**
     * 忘记密码
     *
     * @param user
     */
    public void updatePwd(User user) {
        if (user != null) {
            userDao.updatePwd(user);
        }

    }

    /**
     * 获取紧急联系人列表
     *
     * @param t
     * @return
     */
    @Override
    public PageInfo<User> getUrgentUser(UserEquipmentParameter t) {
        PageInfo<User> info = null;
        try {
            // 分页
            Page<User> tPage = PageHelper.startPage(t.getPageNumber(), t.getPageSize());
            // 获取列表
            List<User> findImageList = userDao.getUrgentUser(t);
            // 获取返回分页数据
            info = new PageInfo(findImageList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

}
